/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.chl.sasanf.crud;

import edu.chl.sasanf.jpa.nbp.Product;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author sasanf
 */
@WebServlet(name = "DatabaseServlet", urlPatterns = {"/dbs"})
public class DatabaseServlet extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String what = request.getParameter("what");
            //if (request.getParameter("what").equals("findAll")) {
            DatabaseImpl prod = new DatabaseImpl("webshop_pu");
            List<Product> list = prod.findAll();



            if (what.equals("findall")) {

                out.println("<table id=\"myTable\" border = \"1\">");
                out.println("<tr><th>" + "ID" + "</th>");
                out.println("<th>" + "Name" + "</th>");
                out.println("<th>" + "Category" + "</th>");
                out.println("<th>" + "Prise" + "</th></tr>");

                for (Product pr : list) {
                    out.println("<tr>");
                    out.println("<td>" + pr.getId() + "</td>");
                    out.println("<td>" + pr.getName() + "</td>");
                    out.println("<td>" + pr.getCat() + "</td>");
                    out.println("<td>" + pr.getPrice() + "</td>");
                    out.println("<td class=\"buttons\">");
                    // out.println(createbtn("Edit", i));
                    //out.println(createbtn("Del", i));
                    out.println("</td>");
                    out.println("</tr>");

                }

                out.println("</table>");
            } else if (what.equals("save")) {

                String pid = request.getParameter("id");
                System.out.println("save-id" + pid);
                String pname = request.getParameter("name");
                String pcat = request.getParameter("cat");
                String testPrice = request.getParameter("price");
                Double pprice;
                if (!testPrice.isEmpty()) {
                    pprice = new Double(testPrice);
                } else {
                    pprice = new Double(0);
                }
                Product testprod = new Product(pname, pcat, pprice);

                if (pid.equals("null")) {

                    prod.create(testprod);


                } else {
                    testprod.setId(new Long(pid));
                    prod.edit(testprod);

                }


            } else if (what.equals("del")) {
                Long pid = new Long(request.getParameter("id"));
                Product p = prod.find(pid);
                prod.remove(p);
            } else {
                out.println("test");
            }

        } finally {
            out.close();
        }

    }

    public String createbtn(String btnType, int i) {

        return "<input type = \"submit\" value=\"" + btnType + "\" id=\"" + btnType + i + "\" />";

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
